import gi
import math
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


class Calculator(Gtk.Window):
    def __init__(self):
        # super().__init__(title="Calculator")
        super().__init__()

        self.num1 = ''
        self.num2 = ''
        self.ans = ''
        self.operators = ''
        self.content = ''
        self.set_resizable(False)

        grid = Gtk.Grid()
        #line 1
        self.entry = Gtk.Entry()
        self.entry.set_alignment(1)
        self.btn_delete = Gtk.Button(label='X')
        self.btn_delete.connect("clicked", self.on_button_delete)

        self.btn_sqrt = Gtk.Button(label="✓")
        self.btn_sqrt.connect("clicked", self.on_button_sqrt)

        self.btn_square = Gtk.Button(label="**")
        self.btn_square.connect("clicked", self.on_button_square)

        self.btn_pi = Gtk.Button(label="PI")
        self.btn_pi.connect("clicked", self.on_button_pi)
        # line 2
        self.btn_1 = Gtk.Button(label="1")
        self.btn_1.connect("clicked", self.on_button_1)

        self.btn_2 = Gtk.Button(label="2")
        self.btn_2.connect("clicked", self.on_button_2)

        self.btn_3 = Gtk.Button(label="3")
        self.btn_3.connect("clicked", self.on_button_3)
        
        self.btn_4 = Gtk.Button(label="4")
        self.btn_4.connect("clicked", self.on_button_4)
        
        self.btn_5 = Gtk.Button(label="5")
        self.btn_5.connect("clicked", self.on_button_5)

        self.btn_6 = Gtk.Button(label="6")
        self.btn_6.connect("clicked", self.on_button_6)

        self.btn_7 = Gtk.Button(label="7")
        self.btn_7.connect("clicked", self.on_button_7)

        self.btn_8 = Gtk.Button(label="8")
        self.btn_8.connect("clicked", self.on_button_8)

        self.btn_9 = Gtk.Button(label="9")
        self.btn_9.connect("clicked", self.on_button_9)

        self.btn_0 = Gtk.Button(label="0")
        self.btn_0.connect("clicked", self.on_button_0)
        # line 3
        self.btn_plus = Gtk.Button(label="+")
        self.btn_plus.connect("clicked", self.on_button_plus)
        
        self.btn_minus = Gtk.Button(label="-")
        self.btn_minus.connect("clicked", self.on_button_minus)
        
        self.btn_multiple = Gtk.Button(label="*")
        self.btn_multiple.connect("clicked", self.on_button_multiple)

        self.btn_divide = Gtk.Button(label="/")
        self.btn_divide.connect("clicked", self.on_button_divide)
        # line 4
        self.btn_percent = Gtk.Button(label="%")
        self.btn_percent.connect("clicked", self.on_button_percent)

        self.btn_point = Gtk.Button(label=".")
        self.btn_point.connect("clicked", self.on_button_point)
       
        self.btn_equal = Gtk.Button(label="=")
        self.btn_equal.connect("clicked", self.on_button_equal)
        
        self.btn_clear = Gtk.Button(label="C")
        self.btn_clear.connect("clicked", self.on_button_clear)
        
        self.btn_minusplus = Gtk.Button(label="-/+")
        self.btn_minusplus.connect("clicked", self.on_button_minusplus)

        # line 1-0
        grid.attach(self.entry, 0, 0, 5, 1)
        # line 1-1
        grid.attach(self.btn_delete, 1, 1, 1, 1)
        grid.attach(self.btn_sqrt, 2, 1, 1, 1)
        grid.attach(self.btn_square, 3, 1, 1, 1)
        grid.attach(self.btn_pi, 4, 1, 1, 1)
        # line 1-2
        grid.attach(self.btn_7, 1, 2, 1, 1)
        grid.attach(self.btn_8, 2, 2, 1, 1)
        grid.attach(self.btn_9, 3, 2, 1, 1)
        grid.attach(self.btn_divide, 4, 2, 1, 1)
        # line 1-3
        grid.attach(self.btn_4, 1, 3, 1, 1)
        grid.attach(self.btn_5, 2, 3, 1, 1)
        grid.attach(self.btn_6, 3, 3, 1, 1)
        grid.attach(self.btn_multiple, 4, 3, 1, 1)
        # line 1-4
        grid.attach(self.btn_1, 1, 4, 1, 1)
        grid.attach(self.btn_2, 2, 4, 1, 1)
        grid.attach(self.btn_3, 3, 4, 1, 1)
        grid.attach(self.btn_minus, 4, 4, 1, 1)
        # line 1-5
        grid.attach(self.btn_0, 1, 5, 1, 1)
        grid.attach(self.btn_point, 2, 5, 1, 1)
        grid.attach(self.btn_percent, 3, 5, 1, 1)
        grid.attach(self.btn_plus, 4, 5, 1, 1)
        # line 1-6
        grid.attach(self.btn_minusplus, 1, 6, 1, 1)
        grid.attach(self.btn_clear, 2, 6, 1, 1)
        grid.attach(self.btn_equal, 3, 6, 1, 1)

        self.add(grid)

    def on_button_delete(self, widget):
        '''
        button_delete
        '''
        if self.entry.get_text_length() == 1:
            self.entry.set_text('')
            self.content = ''
        else:
            self.content = self.entry.get_text()[:-1]
            self.entry.set_text(self.content)


    def on_button_sqrt(self, widget):
        '''
        button_sqrt
        '''
        index = 0
        if self.entry.get_text() != '.':
            num1 = self.entry.get_text()
            self.ans = math.sqrt(float(num1))
            tmp = str(self.ans)                
            index = tmp.index('.')
            ilen = len(tmp[index:])
            if ilen > 8:
                tmp = tmp[:index + 9]
                self.entry.set_text(tmp)
            else:    
                self.entry.set_text(str(self.ans))


    def on_button_square(self, widget):
        '''
        button_square
        '''
        if self.entry.get_text() != '.':
            num1 = self.entry.get_text()
            self.ans = math.pow(float(num1), 2)
            tmp = str(self.ans)                
            index = tmp.index('.')
            ilen = len(tmp[index:])
            if ilen > 8:
                tmp = tmp[:index + 9]
                self.entry.set_text(tmp)
            else:    
                self.entry.set_text(str(self.ans))


    def on_button_pi(self, widget):
        '''
        button_pi
        '''
        self.entry.set_text('3.14')
        self.num1 = '3.14'
        self.content = '3.14'


    def on_button_1(self, widget):
        '''
        button_1
        '''
        if self.entry.get_text() == '0':
            self.content = '1'
            self.entry.set_text(self.content)
        else:
            self.content = self.entry.get_text() + '1'
            self.entry.set_text(self.content)


    def on_button_2(self, widget):
        '''
        button_2
        '''
        if self.entry.get_text() == '0':
            self.content = '2'
            self.entry.set_text(self.content)
        else:
            self.content = self.entry.get_text() + '2'
            self.entry.set_text(self.content)


    def on_button_3(self, widget):
        '''
        button_3
        '''
        if self.entry.get_text() == '0':
            self.content = '3'
            self.entry.set_text(self.content)
        else:
            self.content = self.entry.get_text() + '3'
            self.entry.set_text(self.content)


    def on_button_4(self, widget):
        '''
        button_4
        '''
        if self.entry.get_text() == '0':
            self.content = '4'
            self.entry.set_text(self.content)
        else:
            self.content = self.entry.get_text() + '4'
            self.entry.set_text(self.content)


    def on_button_5(self, widget):
        '''
        button_5
        '''
        if self.entry.get_text() == '0':
            self.content = '5'
            self.entry.set_text(self.content)
        else:
            self.content = self.entry.get_text() + '5'
            self.entry.set_text(self.content)


    def on_button_6(self, widget):
        '''
        button_6
        '''
        if self.entry.get_text() == '0':
            self.content = '6'
            self.entry.set_text(self.content)
        else:
            self.content = self.entry.get_text() + '6'
            self.entry.set_text(self.content)


    def on_button_7(self, widget):
        '''
        button_7
        '''
        if self.entry.get_text() == '0':
            self.content = '7'
            self.entry.set_text(self.content)
        else:
            self.content = self.entry.get_text() + '7'
            self.entry.set_text(self.content)


    def on_button_8(self, widget):
        '''
        button_8
        '''
        if self.entry.get_text() == '0':
            self.content = '8'
            self.entry.set_text(self.content)
        else:
            self.content = self.entry.get_text() + '8'
            self.entry.set_text(self.content)


    def on_button_9(self, widget):
        '''
        button_9
        '''
        if self.entry.get_text() == '0':
            self.content = '9'
            self.entry.set_text(self.content)
        else:
            self.content = self.entry.get_text() + '9'
            self.entry.set_text(self.content)
      

    def on_button_0(self, widget):
        '''
        button_0
        '''
        if self.entry.get_text() == '0':
            self.content = '0'
            self.entry.set_text(self.content)
            
        else:
            self.content = self.entry.get_text() + '0'
            self.entry.set_text(self.content)
   

    def on_button_plus(self, widget):
        '''
        button_plus
        '''
        self.num1 = self.content
        self.operators = '+'
        self.content = ''
        self.entry.set_text('')

    def on_button_minus(self, widget):
        '''
        button_minus
        '''
        self.num1 = self.content
        self.operators = '-'
        self.content = ''
        self.entry.set_text('')

    def on_button_multiple(self, widget):
        '''
        button_multiple
        '''
        self.num1 = self.content
        self.operators = '*'
        self.content = ''
        self.entry.set_text('')      
        
    def on_button_divide(self, widget):
        '''
        button_divide
        '''
        self.num1 = self.content
        self.operators = '/'
        self.content = ''
        self.entry.set_text(self.content)

    def on_button_percent(self, widget):
        '''
        button_percent
        '''
        index = 0
        self.content = self.entry.get_text()
        tmpStr = self.content
        if self.entry.get_text() == '.' or self.entry.get_text() == '':
            return

        if tmpStr.count('.') == 0:
            tmpStr = str(float(tmpStr) * 100)
            index = tmpStr.index('.')
            ilen = len(tmpStr[index:])
            if ilen > 8:
                tmpStr = tmpStr[:index + 9]
                self.entry.set_text(tmpStr)
            else:    
                self.entry.set_text(str(tmpStr))
        else:
            self.content = str(float(self.content) * 100)
            self.entry.set_text(self.content)          

    def on_button_point(self, widget):
        '''
        button_point
        '''
        tmpStr = self.content
        if self.entry.get_text() == '.':
            return

        if tmpStr.count('.') != 0 and tmpStr.index('.') != 0:
            return
        else:
            self.content = self.content + '.'
            self.entry.set_text(self.content)


    def on_button_equal(self, widget):
        '''
        button_equal
        '''
        self.num2 = self.content
        if self.operators == '+':
            self.ans = str(float(self.num1) + float(self.num2))
            self.content = self.ans
            self.entry.set_text(self.ans)
        elif self.operators == '-':
            self.ans = str(float(self.num1) - float(self.num2))
            self.content = self.ans
            self.entry.set_text(self.ans)
        elif self.operators == '*':
            self.ans = str(float(self.num1) * float(self.num2))
            self.content = self.ans
            self.entry.set_text(self.ans)
        elif self.operators == '/' and float(self.num2) != 0.0:
            self.ans = str(float(self.num1) / float(self.num2))
            self.content = self.ans
            self.entry.set_text(self.ans)                

    def on_button_clear(self, widget):
        '''
        button_clear
        '''
        self.entry.set_text('')
        self.content = ''
        self.num1 = ''
        self.num2 = ''
        self.operators = ''            

    def on_button_minusplus(self, widget):
        '''
        button_minusplus
        '''
        if self.entry.get_text() != '0':
            self.content = str(-1 * float(self.entry.get_text()))
            self.entry.set_text(self.content)
                               

win = Calculator()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
